package com.truyen.controller;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.truyen.model.AnhDepModel;
import com.truyen.model.ImgContentAnhDepModel;
import com.truyen.persistence.entity.AnhDep;
import com.truyen.persistence.entity.ContentAnhDep;
import com.truyen.persistence.entity.LoaiAnhDep;
import com.truyen.service.AnhDepService;
import com.truyen.service.LoaiAnhDepService;
import com.truyen.utils.PagingInfo;
import com.truyen.utils.Unsigned;


@Controller
@RequestMapping("/anh-dep")
public class AnhDepController {
	
	@Autowired
	private AnhDepService anhDepService;
	
	@Autowired
	private LoaiAnhDepService loaiAnhDepService;
	
	public void getLstCategories(Model model){
		List<LoaiAnhDep> lstLoaiAnhDep = loaiAnhDepService.getAll();
		model.addAttribute("lstLoaiAnhDep", lstLoaiAnhDep);
	}
	
	@RequestMapping("/ajaxLoadAnhDep.htm")
	@ResponseBody
	public List<AnhDep> ajaxLoadAnhDep(@RequestParam(defaultValue = "1") int curPage, @RequestParam Long idLoaiAnh){
		if(idLoaiAnh != null && idLoaiAnh > 0 && curPage > 0){
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(curPage);
			pInfo.setSizePerPage(3);
			LoaiAnhDep loaiAnhDep = loaiAnhDepService.findById(idLoaiAnh);
			return anhDepService.getLstByType(loaiAnhDep, pInfo, "id", true);
		}else{
			return null;
		}
		
	}
	
	public String getAliasLoaiAnh(long idLoaiAnh){
		return loaiAnhDepService.findById(idLoaiAnh).getAlias().replace(" ", "-");
	}
	
	public AnhDepModel addComicHintModel(Object obj) {
		Object[] arr = (Object[]) obj;
		String idComic = (String) arr[0];
		String nameComic = (String) arr[1];
		String nameUnsignedComic = (String) arr[2];
		String linkImage = (String) arr[3];
		String nameImage = (String) arr[4];
		long idLoaiAnh = (long) arr[5];
		String aliasLoaiAnh = getAliasLoaiAnh(idLoaiAnh);
		String idLoaiAnh1 = Long.toString(idLoaiAnh);
		AnhDepModel chm = new AnhDepModel();
		chm.setIdComic(idComic);
		chm.setNameComic(nameComic);
		chm.setNameUnsignedComic(nameUnsignedComic.replace(" ", "-"));
		chm.setLinkImage(linkImage);
		chm.setNameImage(nameImage);
		chm.setIdLoaiAnh(idLoaiAnh1);
		chm.setAliasLoaiAnh(aliasLoaiAnh);
		return chm;
	}

	public List<AnhDepModel> addLstComicHintModel(List<Object> lstOjb) {
		ArrayList<AnhDepModel> arr = new ArrayList<AnhDepModel>();
		for (Object lst : lstOjb) {
			arr.add(addComicHintModel(lst));
		}
		return arr;
	}
	
	@RequestMapping("/hintAnh.htm")
	@ResponseBody
	public List<AnhDepModel> getHint(
			@RequestParam(defaultValue = "") String kwSearch) {
		if (kwSearch!= null && !kwSearch.equals("")) {
			List<AnhDepModel> lstComicHintModel = addLstComicHintModel(anhDepService.getObjectByNameUnsigned(kwSearch, 10));
			return lstComicHintModel;
		}
		return null;
	}
	
	@RequestMapping("/trang-chu.htm")
	public String home(Model model){
		LoaiAnhDep bikiniGirl = loaiAnhDepService.findById((long) 2);
		LoaiAnhDep girlXinh = loaiAnhDepService.findById((long) 3);
		LoaiAnhDep girlSexy = loaiAnhDepService.findById((long) 5);
		List<AnhDep> lstBikiniGirl = anhDepService.getLstByType(bikiniGirl,10,"id",true);
		List<AnhDep> lstGirlXinh = anhDepService.getLstByType(girlXinh,10,"id",true);
		List<AnhDep> lstGirlSexy = anhDepService.getLstByType(girlSexy,10,"id",true);
		getLstCategories(model);
		model.addAttribute("bikiniGirl", bikiniGirl);
		model.addAttribute("girlXinh", girlXinh);
		model.addAttribute("girlSexy", girlSexy);
		model.addAttribute("lstGirlSexy", lstGirlSexy);
		model.addAttribute("lstGirlXinh", lstGirlXinh);
		model.addAttribute("lstBikiniGirl", lstBikiniGirl);
		return "/anhDep/home/index";
	}
	
	@RequestMapping("/{nameLoaiAnh}/{aliasAlbum}-L{idLoaiAnh}-A{idAnhDep}.htm")
	public String xemAlbum(Model model,
			@PathVariable("idLoaiAnh") Long idLoaiAnh,
			@PathVariable("idAnhDep") Long idAnhDep){
		if(idLoaiAnh != null && idLoaiAnh > 0 && idAnhDep != null && idAnhDep > 0){
			LoaiAnhDep loaiAnhDep = loaiAnhDepService.findById(idLoaiAnh);
			AnhDep anhDep = anhDepService.findById(idAnhDep);
			if(loaiAnhDep != null && anhDep != null){
				ContentAnhDep cad = anhDep.getContentAnhDeps().get(0);
				long views = anhDep.getViews()+1;
				anhDepService.updateViews(idAnhDep,views);
				String lstImg = cad.getContent();
				String[] lsImg = lstImg.split("\\|");
				List<ImgContentAnhDepModel> lstLinkImg = new ArrayList<ImgContentAnhDepModel>();
				int i = 1;
				for(String linkImage : lsImg){
					ImgContentAnhDepModel cadm = new ImgContentAnhDepModel();
					cadm.setLinkImage(linkImage);
					cadm.setStt(i);
					i = i + 1;
					lstLinkImg.add(cadm);
				}
				List<AnhDep> lstAnhDep = anhDepService.getListRandom(loaiAnhDep,9,true);
				getLstCategories(model);
				model.addAttribute("loaiAnhDep", loaiAnhDep);
				model.addAttribute("cad", cad);
				model.addAttribute("lstLinkImg", lstLinkImg);
				model.addAttribute("lstAnhDep", lstAnhDep);
				model.addAttribute("anhDep", anhDep);
				return "/anhDep/detail/index";
			}
		}
		return "redirect:/anh-dep/bikini-girl-L2-trang-1.htm";
		
	}
	
	@RequestMapping("/{nameLoaiAnh}-L{idLoaiAnh}-trang-{curPage}.htm")
	public String detail(Model model,
			@PathVariable("idLoaiAnh") Long idLoaiAnh,@PathVariable("curPage") int curPage){
		if(idLoaiAnh != null && idLoaiAnh > 0){
			LoaiAnhDep loaiAnhDep = loaiAnhDepService.findById(idLoaiAnh);
			if(loaiAnhDep != null){
				PagingInfo pInfo = new PagingInfo();
				pInfo.setCurrentPage(curPage);
				pInfo.setSizePerPage(12);
				List<AnhDep> lstAnhDep = anhDepService.getLstByType(loaiAnhDep, pInfo, "id", true);
				getLstCategories(model);
				model.addAttribute("loaiAnhDep", loaiAnhDep);
				model.addAttribute("lstAnhDep", lstAnhDep);
				model.addAttribute("idLoaiAnh", idLoaiAnh);
				model.addAttribute("pInfo", pInfo);
				return "/anhDep/album/index";
			}
		}
		return "redirect:/anh-dep/bikini-girl-L2-trang-1.htm";
	}
	
	//trang tim kiem
	@RequestMapping("/tim-kiem-anh.htm")
	public String timKiemTruyen(Model model,
			@RequestParam(defaultValue="") String k,
			@RequestParam(defaultValue="1") int p) {
		if(k != null && !k.equals("") && p > 0){
			model.addAttribute("k", k);
			k = k.replace("script", "");
			k = Unsigned.boDau(k);
			model.addAttribute("kUnsigned", k);
			PagingInfo pInfo = new PagingInfo();
			pInfo.setCurrentPage(p);
			pInfo.setSizePerPage(12);
			List<AnhDep> lstAnhDep = anhDepService.getLstByPropertyWithPaging("alias", k, "id", pInfo, true);
			model.addAttribute("lstAnhDep", lstAnhDep);
			model.addAttribute("pInfo", pInfo);
			model.addAttribute("countRecord", pInfo.getCount());
			return "/anhDep/search/index";
		}else{
			return "redirect:/anh-dep/bikini-girl-L2-trang-1.htm";
		}
	}
	
}